package com.douyu.water.core.web.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.douyu.water.core.Configure;
import com.douyu.water.core.DwzResult;
import com.douyu.water.core.PagePara;
import com.douyu.water.core.model.Area;
import com.douyu.water.core.model.Menu;
import com.douyu.water.core.model.Role;
import com.douyu.water.core.model.Rtu;
import com.douyu.water.core.model.User;
import com.douyu.water.core.service.EquiService;
import com.douyu.water.core.service.RoleService;
import com.douyu.water.core.service.UserService;
import com.douyu.water.util.JsonUtil;

@Controller
@RequestMapping("/user/*")
public class UserController {


	@Autowired
	private UserService userService;
	
	@Autowired
	private RoleService roleService;
	@Resource
	private EquiService equiService;
	
	
	/**
	 * 获取菜单列表
	 * @param request
	 * @param menu User
	 * @param page PagePara
	 * @return
	 */
	@RequestMapping("/select")
	public String select(HttpServletRequest request,User menu,PagePara page)
	{
		String result = "WEB-INF/jsp/user/list";
		try
		{
			List<User> list = userService.select(menu, page);
			request.setAttribute("list", list);
			request.setAttribute("menu", menu);
			request.setAttribute("pagePara", page);
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 跳转到添加页面
	 * @param request
	 * @return
	 */
	@RequestMapping("add")
	public String add(HttpServletRequest request)
	{
		String result ="WEB-INF/jsp/user/add";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			List<Role> list = roleService.select(null, null);
			request.setAttribute("role", list);
		}
		catch(Exception e){
			e.printStackTrace();
			
		}
		return result;
	}
	
	/**
	 * 新建
	 * @param request
	 * @param menu
	 * @return
	 */
	@RequestMapping("/insert")
	public String insert(HttpServletRequest request,User user)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			user.setCreateuser(((User)(request.getSession().getAttribute("loginUser"))).getUcode());
			int flag = userService.insert(user);
			switch(flag)
			{
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("添加失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("添加成功");
				dwzResult.setNavTabId("userList");
				dwzResult.setCloseCurrent(true);
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("名称重复，添加失败");
				break;
			}	
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		return DwzResult.URL;
	}
	
	/**
	 * 修改页面
	 * @param request
	 * @param record User  
	 *        -菜单  ，只用ucode
	 * @return
	 */
	@RequestMapping("edit")
	public String edit(HttpServletRequest request,User record)
	{
		String result = "WEB-INF/jsp/user/edit";
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			User menu = userService.view(record);
			if(menu != null)
			{
				List<Role> list = roleService.select(null,null);
				userService.checkRole(list,menu);
				request.setAttribute("role", list);
				request.setAttribute("usr",menu);
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("未能获取对象");
				return DwzResult.URL;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
			return DwzResult.URL;
		}
		return result;
	}
	
	/**
	 * 更新用户信息
	 * @param request
	 * @param record User
	 * @return
	 */
	@RequestMapping("/update")
	public String update(HttpServletRequest request,User record)
	{
		DwzResult dwzResult= new DwzResult(); 
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			int flag = userService.update(record);
			switch(flag)
			{
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("更新失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("更新成功");
				dwzResult.setCloseCurrent(true);
				dwzResult.setNavTabId("userList");
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("名称重复，更新失败");
				break;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}
	/**
	 * 删除
	 * @param request
	 * @param record User   
	 *        -用户编号
	 * @return  String
	 */
	@RequestMapping("/delete")
	public String delete(HttpServletRequest request,User record)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			int flag = userService.delete(record);
			switch(flag)
			{
			case 0:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("删除失败");
				break;
			case 1:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("删除成功");
				dwzResult.setCloseCurrent(false);
				dwzResult.setNavTabId("userList");
				break;
			case 2:
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("正在使用，无法删除");
				break;
			}
		}
		catch(Exception e){
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}
	
	@RequestMapping("/toUpdatePwd")
	public String toUpdatePwd(HttpServletRequest request)
	{
		String result ="WEB-INF/jsp/user/pwd";
		return result;
	}
	@RequestMapping("/updatePwd")
	public String updatePwd(HttpServletRequest request,User user)
	{
		DwzResult dwzResult = new DwzResult();
		request.setAttribute("dwzResult", dwzResult);
		try
		{
			User login = (User)(request.getSession().getAttribute(Configure.LOGIN_USER)); 
			user.setUcode(login.getUcode());
			int flag = userService.updateSelective(user);
			if(flag > 0)
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_SUCCESS);
				dwzResult.setMessage("修改成功");
				dwzResult.setCloseCurrent(true);
			}
			else
			{
				dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
				dwzResult.setMessage("修改失败");
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			dwzResult.setStatusCode(DwzResult.STATUS_CODE_ERROR);
			dwzResult.setMessage("系统错误:" + e.getMessage());
		}
		return DwzResult.URL;
	}
	@RequestMapping("/login")
	public String login(HttpServletRequest request,HttpServletResponse response,User user)
	{
		String result ="index";
		try
		{
			User login = userService.login(user);
			if(login == null){		
				request.setAttribute("errMsg", "用户名或密码错误，请重新登录");
				result ="redirect:/login.jsp?errMsg=用户名或密码错误，请重新登录";
				return result;
			}
			List<Rtu> list = equiService.select(null, null,login.getUcode());			
//			检索地区列表
			List<Area> areaList = equiService.selectArea(login.getUcode().trim());
			String str = JsonUtil.convertListToJSONArray(list, null).toString();
			request.setAttribute("data", str);
			request.setAttribute("areaList", areaList);
			request.setAttribute("list", list);
			request.setAttribute("zoom", areaList.size());
			if(login.getUcode().equals("1")){
				request.setAttribute("lng", "116.98");
				request.setAttribute("lat", "36.67");
			}
			else if(list.size()>0&&list!=null){
				String areacode=list.get(0).getStationnumber().substring(0,4);
				areacode+="00";
				Area area = equiService.selectSingArea(areacode);
				request.setAttribute("lng",area.getLng());
				request.setAttribute("lat",area.getLat());		
				
			}
			else if(areaList != null && areaList.size() > 0)
			{
				request.setAttribute("lng", areaList.get(0).getLng());
				request.setAttribute("lat", areaList.get(0).getLat());
			}
			else
			{
				request.setAttribute("lng", "116.98");
				request.setAttribute("lat", "36.67");				
			}
			//获取最大变化量
			String max = equiService.selectResult();
			request.setAttribute("max", max);
			request.getSession().setAttribute(Configure.LOGIN_USER, login);
			List<Menu> menu = userService.selectMenu(login);
			request.setAttribute("menu", menu);
			request.setAttribute("username", login.getRealname());
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request)
	{
		String result = "redirect:/login.jsp";
		request.getSession().removeAttribute("loginUser");
		request.getSession().invalidate();
		return result;
	}
}
